package com.tanhua.dubbo.api.impl;

import com.mongodb.client.result.UpdateResult;
import com.tanhua.domain.mongo.Voice;
import com.tanhua.dubbo.api.VoiceApi;
import net.sf.jsqlparser.expression.LongValue;
import org.apache.dubbo.config.annotation.Service;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.util.List;
import java.util.Random;
import java.util.concurrent.ThreadLocalRandom;

@Service(timeout = 10000000)
public class VoiceApiImpl implements VoiceApi {

    @Autowired
    private MongoTemplate mongoTemplate;


    //    保存语音
    @Override
    public void save(Voice voice) {
        mongoTemplate.save(voice);
    }

//    //    修改剩余次数
//    @Override
//    public Voice getremainingTimes(Long userId,Integer remainingTimes) {
//        Query query = new Query(Criteria.where("userId").is(userId));
//        Update update = new Update();
//        update = update.set("remainingTimes", (remainingTimes - 1));
//        mongoTemplate.upsert(query, update, Voice.class);
//        Query query1 = new Query(Criteria.where("userId").is(userId));
//        Voice voice = mongoTemplate.findOne(query1, Voice.class);
//
//
//        return voice;
//    }

    //    随机获取所有语音
    @Override
    public Voice findVid(Long userId) {
//        查询用户id不等于自己的语音
        Query query = new Query(Criteria.where("userId").ne(userId));

        List<Voice> voiceList = mongoTemplate.find(query, Voice.class);

        Random random = new Random();
//        Voice voice = new Voice();
//        if (voiceList.size() > 0) {
//             voice = voiceList.get(random.nextInt(voiceList.size()));
//        }
        Voice voice = voiceList.get(random.nextInt(voiceList.size()));
        return voice;
    }

    //    删除获取到的语音
    @Override
    public void delete(ObjectId id) {
        Query query = new Query(Criteria.where("_id").is(id));
        mongoTemplate.remove(query, Voice.class);
    }

//    根据语音id，查找语音信息
    @Override
    public Voice findById(Long id) {
        Query query = new Query(Criteria.where("_id").is(id));
        Voice voice = mongoTemplate.findOne(query, Voice.class);
        return voice;
    }

////    定时类，定时更新接收次数
//    @Override
//    public void updateremainingTimes(Long userId) {
//        Query query = new Query(Criteria.where("_id").is(userId));
//        Update update = new Update();
//        update.set("remainingTimes",10);
//
//        mongoTemplate.upsert(query,update,Voice.class);
//    }

////    修改接收次数
//    @Override
//    public void update(Long userId,Integer remainingTimes) {
//        Query query = new Query(Criteria.where("_id").is(userId));
//        Update update = new Update();
//        update.set("remainingTimes",remainingTimes-1);
//        mongoTemplate.upsert(query,update,Voice.class);
//    }

//    根据当前用户id查询语音信息
    @Override
    public Voice findByUserId(Long userId) {
        Query query = new Query(Criteria.where("userId").is(userId));
        Voice voice = mongoTemplate.findOne(query, Voice.class);
        return voice;
    }

}
